package reporting;

import java.util.*;

import core.SeleneseTestCaseEx;
import util.TestConfiguration;
import util.Util;
import static util.ConstantCollection.*;

class TxtReport extends TemplateReport{
	private final Integer ID_LEN = 5;
	private final Integer NAME_LEN = 50;
	private final Integer DETAILS_LEN = 100;
	private final Integer STATUS_LEN = 11;
	private final Integer TIME_LEN = 8;
	private final Integer COUNT_COLUMN = 6;
	private final Integer FULL_LEN = ID_LEN + NAME_LEN + DETAILS_LEN + STATUS_LEN + TIME_LEN + COUNT_COLUMN;
	
	private FileWork fw = FileWork.getInstance();
	private TestConfiguration tc = TestConfiguration.getInstance();
	private String pathToLog = tc.getEnv("_logpath")+ "/txt/" + getName() + ".txt";
	
	public TxtReport(String name) {
		super(name);
		String tempPath;
		String headerText;
		String tableHeader;
		tempPath = tc.getEnv("_logpath")+ "/txt/";
		
	    if(!fw.existsFileOrDir(tempPath)){
	    	fw.createDir(tempPath);
		}
	    
	    tempPath = tempPath + getName() + ".txt";
	    
	    if(!fw.existsFileOrDir(tempPath)){
	    	fw.createFile(tempPath);
			Util u = new Util();
			
			//Header log
			headerText = "Test title:\t" + tc.getEnv("_testtitle") + "\n";
			headerText = headerText + "Test name:\t" + tc.getEnv("_testname") + "\n";
			headerText = headerText + "Server:\t" + SeleneseTestCaseEx.getServerName() + "\n";
			headerText = headerText + "Browser:\t" + SeleneseTestCaseEx.getBrowser() + "\n";
			headerText = headerText + "Time zone:\t" + u.getTimeZone() + "\n";
			headerText = headerText + "Text log started:\t" + u.getTimeNow() + ".\n";
			
			//Header for table
	    	tableHeader = repeat('-', FULL_LEN) + "\n";
	    	tableHeader = tableHeader + "|" + addSpaceToString("ID", ID_LEN) + "|"; 
	    	tableHeader = tableHeader + addSpaceToString("NAME", NAME_LEN) + "|";
	    	tableHeader = tableHeader + addSpaceToString("DETAILS", DETAILS_LEN) + "|";
	    	tableHeader = tableHeader + addSpaceToString("STATUS", STATUS_LEN) + "|";
	    	tableHeader = tableHeader + addSpaceToString("TIME", TIME_LEN) + "|";
	    	tableHeader = tableHeader + "\n" + repeat('-', FULL_LEN) + "\n";
	    	
	    	fw.appendFile(tempPath, headerText + tableHeader);
	    }
	    
	}
	
	/** Increase text to specify length. If length text more specify length,
	 * then text will truncated
	 * @param text Some text
	 * @param len Length input text after processing 
	 * @return Appended text by space
	 */
	private String addSpaceToString(String text, Integer len) {
		if (text.length() > len) {
			text = text.substring(1, len);
		}
		
		return text + repeat(' ', len - text.length());
	}

	/** Report all information from step of TC to txt file
	 * @param step Step of testcase
	 */
	public void reportStep(TCStep step) {
		String text;
		final String ACTION_TITLE = "Input/Action";
		final String EXPECTED_TITLE = "Expected Result";
		Util u = new Util();
		
		//Generate Details text
		String detail = "";
    	String lastTypeItem = "action";
    	
    	for (Iterator<StepItem> it = step.getItemCollection().iterator(); it.hasNext(); ) {
    		StepItem item = it.next(); 
    		String lineSting = "";
    		
    		if (item.getType() == "action") {
           	 	if (lastTypeItem == "cp") {
           	 		lastTypeItem = "action";
           	 		detail = detail + "|" + repeat(' ', ID_LEN) + "|" + repeat(' ', NAME_LEN) + "|" + 
           		          addSpaceToString(ACTION_TITLE, DETAILS_LEN) + "|" + repeat(' ', STATUS_LEN) + 
           		          "|" + repeat(' ', TIME_LEN) + "|\n";
           	 	}
           	 
           	 	lineSting = " *" + getStatusName(item.getStatus()) + "* "
           	 			+ item.getStepNum() + ". " + item.getDescription();
           	 
            } else if (item.getType() == "cp") {
           	 	if (lastTypeItem == "" || lastTypeItem == "action") {
           	 		lastTypeItem = "cp";
           	 		detail = detail + "|" + repeat(' ', ID_LEN) + "|" + repeat(' ', NAME_LEN) + "|" + 
           	 				addSpaceToString(EXPECTED_TITLE, DETAILS_LEN) + "|" + repeat(' ', STATUS_LEN) + 
           	 				"|" + repeat(' ', TIME_LEN) + "|\n";

           	 	}
           	 
           	 	lineSting = " *" + getStatusName(item.getStatus()) + "* "
           	 			 + item.getDescription();
            }
            
            detail = detail + "|" + repeat(' ', ID_LEN) + "|" + repeat(' ', NAME_LEN) + "|" + 
            		  addSpaceToString(lineSting, DETAILS_LEN) + "|" + repeat(' ', STATUS_LEN) + 
	                  "|" + repeat(' ', TIME_LEN) + "|\n";
    		
    	}
    	
		//Generate summary text
		text = "|" + addSpaceToString(step.getId(), ID_LEN) + "|"; 
    	text = text + addSpaceToString(step.getName(), NAME_LEN) + "|";
    	text = text + addSpaceToString(ACTION_TITLE, DETAILS_LEN) + "|";
    	text = text + addSpaceToString(getStatusName(step.getStatus()), STATUS_LEN) + "|";
    	text = text + addSpaceToString(u.getTime(step.getTime()), TIME_LEN) + "|\n" + detail + repeat('-', FULL_LEN) + "\n"; 
		
		if(fw.existsFileOrDir(pathToLog)){
			fw.appendFile(pathToLog, text);
	    }
	}
	
	/** repeat symbol n number
	 * @param symbol Any symbol
	 * @param n Number for repeat symbol
	 * @return String from n symbols
	 */
	private String repeat(char symbol, Integer n) {
        char[] temp = new char[n];
        for (int i = 0; i < n; i++)
            temp[i] = symbol;
        return new String(temp);
    }

}
